//
//  NetworkHandle.m
//  UI_新版豆瓣
//
//  Created by dlios on 15-7-31.
//  Copyright (c) 2015年 lanou3g.com 蓝鸥科技. All rights reserved.
//

#import "NetworkHandle.h"

@implementation NetworkHandle

/* GET异步 */
+ (void)GETWithURL:(NSString *)urlStr complication:(void(^)(id result))block
{
    NSString *str = [urlStr stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
    NSURLRequest *request = [NSURLRequest requestWithURL:[NSURL URLWithString:str]];
    [NSURLConnection sendAsynchronousRequest:request queue:[NSOperationQueue mainQueue] completionHandler:^(NSURLResponse *response, NSData *data, NSError *connectionError) {
        
        /* JSON 解析 */
        NSError *error = nil;
        id result = [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingMutableContainers error:&error];
        /* block 回调 */
        block(result);
        
    }];
}

/* POST异步*/
+ (void)POSTWithURL:(NSString *)urlStr httpBody:(NSString *)httpBody complication:(void(^)(id result))block
{
    /* 转码 */
    NSString *str = [urlStr stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
    NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:[NSURL URLWithString:str]];
    
    request.HTTPMethod = @"POST";
    
    request.HTTPBody = [httpBody dataUsingEncoding:NSUTF8StringEncoding];
    
    
    [NSURLConnection sendAsynchronousRequest:request queue:[NSOperationQueue mainQueue] completionHandler:^(NSURLResponse *response, NSData *data, NSError *connectionError) {
       
        /* JSON Parser */
        NSError *error = nil;
        id result = [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingMutableContainers error:&error];
        
        
        /* block 回调 */
        
        block(result);
        
    }];
}


+ (void)GETWithURL:(NSString *)urlStr delegate:(id)delegate
{
    NSString *str = [urlStr stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
    NSURLRequest *request = [NSURLRequest requestWithURL:[NSURL URLWithString:str]];
    [NSURLConnection sendAsynchronousRequest:request queue:[NSOperationQueue mainQueue] completionHandler:^(NSURLResponse *response, NSData *data, NSError *connectionError) {
        
        /* JSON 解析 */
        NSError *error = nil;
        id result = [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingMutableContainers error:&error];
        
        /* 向代理人发送消息，执行协议方法 */
        [delegate didFinishLoadDate:result];
    }];
}


+ (void)POSTWithURL:(NSString *)urlStr httpBody:(NSString *)httpBody delegate:(id<NetworkHandleDelegate>)delegate
{
    NSString *str = [urlStr stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
    NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:[NSURL URLWithString:str]];
    
    request.HTTPMethod = @"POST";
    
    request.HTTPBody = [httpBody dataUsingEncoding:NSUTF8StringEncoding];
    
    
    [NSURLConnection sendAsynchronousRequest:request queue:[NSOperationQueue mainQueue] completionHandler:^(NSURLResponse *response, NSData *data, NSError *connectionError) {
        
        /* JSON Parser */
        NSError *error = nil;
        id result = [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingMutableContainers error:&error];
        
        
        [delegate didFinishLoadDate:result];
        
    }];

}

@end
